namespace BM.Tools.WebControls.MForm.Controls.Helper
{
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    /// <summary>
    /// A simple web control that marks where more items of the given <see cref="Offspring"/> may be placed in html.
    /// </summary>
    public class Marker : WebControl
    {
        private const string MarkerCssClass = "Mk";

        /// <summary>
        /// Initializes a new instance of the <see cref="Marker"/> class. 
        /// </summary>
        /// <param name="owner">
        /// The owner.
        /// </param>
        public Marker(Offspring owner)
        {
            Owner = owner;
        }

        /// <summary>
        /// Gets Owner.
        /// </summary>
        /// <value>
        /// The owner.
        /// </value>
        public Offspring Owner
        {
            get; 
            private set;
        }

        /// <summary>
        /// Gets ClientID.
        /// </summary>
        /// <value>
        /// The client id.
        /// </value>
        public override string ClientID
        {
            get
            {
                return String.Concat(Owner.CreateClientID(1), "__", MarkerCssClass);
            }
        }

        /// <summary>
        /// Gets or sets Visible.
        /// </summary>
        /// <value>
        /// The visible.
        /// </value>
        public override bool Visible
        {
            get
            {
                if (Owner.MinOccurs == Owner.MaxOccurs)
                {
                    return false;
                }

                if (Owner is Leaf && Owner.MaxOccurs == 1)
                {
                    return false;
                }

                return base.Visible;
            }

            set
            {
                base.Visible = value;
            }
        }

        /// <summary>
        /// Gets TagKey.
        /// </summary>
        /// <value>
        /// The tag key - <see cref="HtmlTextWriterTag.Div" />.
        /// </value>
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        /// <summary>
        /// adds id and css class attributes
        /// </summary>
        /// <param name="writer">
        /// The writer.
        /// </param>
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, MarkerCssClass);
            writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
        }
    }
}